
/**
 * 用户列表返回数据实体
 */
interface UserListItemResponse {
    /**
     * 标识符
     */
    id:number;
    /**
     * 序列号
     */
    index: number;
    /**
     * 用户名
     */
    userName: string;
    /**
     * 手机号
     */
    phoneNumber: string;
    /**
     * 用户组名称
     */
    userGroup: string;
    /**
     * 创建时间
     */
    createTime: string;
    /**
     * 最后登录时间
     */
    lastLoginTime: string;
    /**
     * 是否启用
     */
    isEnabled: boolean
}

/**
 * 用户详情返回数据实体
 */
interface UserDetailResponse extends BaseResponse {
    /**
     * 真实姓名
     */
    realName:string;
    /**
     * 融云头像
     */
    rongAvatar:string;
    /**
     * 标识符
     */
    extData:string;
    /**
     * 错误信息
     */
    errorMsg:string;
    /**
     * 用户名
     */
    userName: string;
    /**
     * 邮箱
     */
    email: string;
    /**
     * 手机号
     */
    phoneNumber: string;
    /**
     * 是否启用
     */
    isEnabled: boolean;
    /**
     * 用户组编号
     */
    userGroupId: string;
    /**
     * 权限数组
     */
    roles: Array<string>;
}
/**
 * 添加头像附件
 */
interface AddUserInfoAttachmentDetail {
    /**
     * attachmentId　附件编号
     */
    attachmentId: string;
    /**
     * attachmentName　名称
     */
    attachmentName: string;
    /**
     * attachmentPath 路径
     */
    attachmentPath: string;
    /**
     * attachmentTag 标签
     */
    attachmentTag: string
}

/**
 * 验证申领密码
 */
interface checkApplyPasswordResponse extends BaseResponse{
    /**
     * 密码是否正确
     */
    isRightPassword:boolean;
    /**
     * 申领tonken
     */
    applyToken:string;
}

/**
 * 根据原申请密码修改申领密码 
 */
interface updateApplyPassword extends BaseResponse{
    /**
     * 用户编号
     */
    id:string;
    /**
     * 原密码
     */
    oldPassword:string;
    /**
     * 密码
     */
    password:string;
    /**
     * 重复密码
     */
    rePassword:string;
}
/**
 * 用户组权限
 */
interface  UserAuth {
    //高等级权限
    HighAuthNumber:number[];
    //中等级权限
    MiddleAuthNumber:number[];
    //低等级权限
    LowAuthNumber:number[];
    //二级权限标签
    SecAuthTypeTag:string[]
}
/**
 *用户服务
 */
interface IUserService {
    /**
     * 获取用户列表
     * v1.0
     * @param userName 用户名
     * @param phoneNumber 手机号
     * @param isEnabled 是否启用
     * @param userGroupId 用户组编号
     */
    getList(userName: string, phoneNumber: string, isEnabled: boolean, userGroupId: string, skip: number, count: number): ng.IHttpPromise<ListResponse<UserListItemResponse>>;

    /**
     * 获取用户详细
     * v1.0
     * @param id 用户编号
     */
    getDetail(id: string): ng.IHttpPromise<UserDetailResponse>;

    /**
     * 编辑用户
     * v1.0
     * @param id 用户编号
     * @param password 密码
     * @param rePassword 重复密码
     * @param email 邮箱
     * @param phoneNumber 手机号
     * @param isEnabled 是否启用
     * @param userGroupId 用户组编号
     * @param roles 权限数组
     */
    edit(id: string, password: string, rePassword: string, email: string, phoneNumber: string, isEnabled: boolean, userGroupId: string, roles: Array<string>,highAuthNumber,middleAuthNumber,lowAuthNumber,secAuthorizationTypeTag:string[]): ng.IHttpPromise<BaseResponse>;

    /**
     * 修改密码
     * v1.0
     * @param id 需要编辑的用户编号
     * @param oldPassword 原密码
     * @param password 密码
     * @param rePassword 重复密码
     */
    updatePassword(id: string, oldPassword: string, password: string, rePassword: string,attachmentHead: Array<AddUserInfoAttachmentDetail>): ng.IHttpPromise<BaseResponse>;

    /**
     * 添加用户
     * v1.0
     * @param userName 用户名
     * @param password 密码
     * @param rePassword 重复密码
     * @param email 邮箱
     * @param phoneNumber 手机号
     * @param isEnabled 是否启用
     * @param userGroupId 用户组编号
     * @param roles 权限组
     */
    add(userName: string, password: string, rePassword: string, email: string, phoneNumber: string, isEnabled: boolean, userGroupId: string, roles: Array<string>): ng.IHttpPromise<BaseResponse>;

    /**
     * 获取容云Token
     * v1.0
     */
    getRongToken(): ng.IHttpPromise<BaseResponse>;

    /**
     * 验证申领密码
     */
    checkApplyPassword(applyPassword:string):ng.IHttpPromise<checkApplyPasswordResponse>;

    /**
     * 根据原申请密码修改申请密码
     * @param id 用户编号
     * @param oldPassword 原密码
     * @param password 密码
     * @param rePassword 重复密码
     */
    updateApplyPassword(id: string, oldPassword: string, password: string, rePassword: string): ng.IHttpPromise<BaseResponse>;

    /**
     * 编辑用户权限
     * v1.0
     * @param id 用户编号
     * @param body 权限
     */
    editAuth:(id:string,body:UserAuth)=>ng.IHttpPromise<BaseResponse>;

    // /**
    //  * 头像
    //  */
    // updateHead(userId: string, path: string, name: string): ng.IHttpPromise<BaseResponse>
}


class userService implements IUserService {
    constructor(private http: ng.IHttpService, private route: routeService) {
    }

    getList(userName: string, phoneNumber: string, isEnabled: boolean, userGroupId: string, skip: number, count: number): ng.IHttpPromise<ListResponse<UserListItemResponse>> {
        return this.http<ListResponse<UserListItemResponse>>({
            method: "GET",
            url: this.route.getUrl("user?userName={{userName}}&phoneNumber={{phoneNumber}}&isEnabled={{isEnabled}}&userGroupId={{userGroupId}}&skip={{skip}}&count={{count}}", {
                "userName": userName,
                "phoneNumber": phoneNumber,
                "isEnabled": isEnabled,
                "userGroupId": userGroupId,
                "skip": skip,
                "count": count
            })
        });
    }

    getDetail(id: string): ng.IHttpPromise<UserDetailResponse> {
        return this.http<UserDetailResponse>({
            method: "GET",
            url: this.route.getUrl("user/{{id}}", {
                id: id
            })
        });
    }

    edit(id: string, password: string, rePassword: string, email: string, phoneNumber: string, isEnabled: boolean, userGroupId: string, roles: Array<string>,highAuthNumber,middleAuthNumber,lowAuthNumber,secAuthorizationTypeTag:string[]): ng.IHttpPromise<BaseResponse> {
        return this.http<BaseResponse>({
            method: "PUT",
            url: this.route.getUrl("user/{{id}}", {
                id: id
            }),
            data: {
                password: password,
                rePassword: rePassword,
                email: email,
                phoneNumber: phoneNumber,
                isEnabled: isEnabled,
                userGroupId: userGroupId,
                roles: roles,
                highAuthNumber:highAuthNumber,
                middleAuthNumber:middleAuthNumber,
                lowAuthNumber:lowAuthNumber,
                SecAuthTypeTag:secAuthorizationTypeTag
            }
        });
    }

    updatePassword(id: string, oldPassword: string, password: string, rePassword: string,attachmentHead: Array<AddUserInfoAttachmentDetail>): ng.IHttpPromise<BaseResponse> {
        return this.http<BaseResponse>({
            method: "PUT",
            url: this.route.getUrl("user/updatePassword"),
            data: {
                id: id,
                oldPassword: oldPassword,
                password: password,
                rePassword: rePassword,
                Head:attachmentHead[0]
                // attach11:attach11
            }
        });
    }

    add(userName: string, password: string, rePassword: string, email: string, phoneNumber: string, isEnabled: boolean, userGroupId: string, roles: Array<string>): ng.IHttpPromise<BaseResponse> {
        return this.http<BaseResponse>({
            method: "POST",
            url: this.route.getUrl("user"),
            data: {
                userName: userName,
                password: password,
                rePassword: rePassword,
                email: email,
                phoneNumber: phoneNumber,
                isEnabled: isEnabled,
                userGroupId: userGroupId,
                roles: roles
            }
        });
    }

    getRongToken(): ng.IHttpPromise<BaseResponse> {
        return this.http<BaseResponse>({
            method: "GET",
            url: this.route.getUrl("user/getRongToken")
        });
    };

    checkApplyPassword(applyPassword:string):ng.IHttpPromise<checkApplyPasswordResponse>{
        return this.http<checkApplyPasswordResponse>({
            method:'GET',
            url:this.route.getUrl("user/CheckApplyPassword?applyPassword={{applyPassword}}",{
                applyPassword:applyPassword
            })
        })
    }
    updateApplyPassword(id: string, oldPassword: string, password: string, rePassword: string): ng.IHttpPromise<BaseResponse> {
        return this.http<BaseResponse>({
            method: "PUT",
            url: this.route.getUrl("user/updateApplyPassword"),
            data: {
                id: id,
                oldPassword: oldPassword,
                password: password,
                rePassword: rePassword
            }
        });
    }
    /**编辑权限 */
    editAuth(id:string,body:UserAuth):ng.IHttpPromise<BaseResponse>{
        return this.http<BaseResponse>({
            method:"PUT",
            url:this.route.getUrl("user/SaveUserAuth?id={{id}}",{id:id}),
            data:body
        })
    }


}
angular.module("userService",[]).service("userService",['$http','routeService',userService]);